var obj;
var list;
var leaves_list;

Page.onload = function() {
    new FormSender('logout', 'auth/logout', 'reload');

    list = new ListController({
		name: 'item',
		view_type: getParam('view_type'),
        url: 'objects/roll',
        container: $('roll_container'),
        action: function(id) {
			hide($('leaves_list'));
			obj.setParams({'id':id}).update()
			},
        setFormParams: function(params) {obj.setFormParams(params)},
		onLeavesLoad: function(id) {	
			list.selectLeaf(id);				
			show($('leaves_list'));
			leaves_list.setParams({'parent_id':id});
			setParam('catalog_parent', id);
			leaves_list.update();
			leaves_list.selectItem(list.params.subitem);
			list.params.subitem = 0;
		},
		subitem: 0
    })

	//leaves_list = new ListController({});
	
	leaves_list = new ListController({
		name: 'leaf',
		view_type: 'flat',
        url: 'objects/leaves',
        container: $('leaves_container'),
        action: function(id) {
			obj.setParams({'id': id, 'catalog_parent': getParam('catalog_parent')}).update();
            },
        setFormParams: function(params) {}
    })

	//Ну да, ну да. Понимаю, очень смешно
	leaves_obj = new ObjectController({
		url: 'objects/form',
        container: $('form_container'),
        action: function() {leaves_list.update()},
        onNew: function(id) {leaves_list.selectItem(id)}
	})

    obj = new ObjectController({
        url: 'objects/form',
        container: $('form_container'),
		action: function() {list.update()},
		onNew: function(id) {list.selectItem(id)}
    })

	obj.setParams(getParams());	
	list.setParams(getParams()).update();

	var loc = urlParams([0, false]);	
    var id = loc[0];
	var sub_id = loc[1]

	if (id != 0 || getParam('is_new')) {	
		list.selectItem(id);		
	}

	if (sub_id != false) {
		list.params.subitem = sub_id;
		list.getLeaves(id);
	}
}

Page.responseCallback = function (transport) {
    var response = transport.responseText.evalJSON();
    var status = response.message;      
    $('form_container').update(status);
    list.unselect().update();
}

Page.onDelete = function() {
    request('objects/delete', {'id': getValue('fs_id'), 'class_name': getParam('class_name')}, Page.responseCallback);
}